<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@page import="support.*,java.util.*,java.sql.*,javax.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
	<%
		response.setContentType("text/html");
		String url = "jdbc:postgresql://localhost:5432/Applications";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		String instruc = null;

		try {
			// Registering Postgresql JDBC driver
			Class.forName("org.postgresql.Driver");
			// Open a connection to the database
			conn = DriverManager.getConnection(url, "postgres", "postgres");
			// Create the statement
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE);

			String loc = session.getAttribute("location").toString();
			System.out.println(loc+"111");
			String univ = request.getParameter("university").toString();
			System.out.println(univ+"222");
			
			int univid = 0, addNew = 0;

			rs = stmt.executeQuery("SELECT * FROM Universities");
			while (rs.next()) {
				if (rs.getInt("ID") > univid) {
					univid = rs.getInt("ID");
				}

				if (univ.equals(rs.getString("University"))) {
					addNew = 0;
					break;
				} else {
					addNew = 1;
				}
			}
			
			out.clear(); 
			if (addNew == 0) {
				out.print("Already exists!");
			}
		} catch (SQLException e) {
			System.out.println(e);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// close connection
		try {
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	%>